<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>组件通信-父子组件通信案例（watch实现）</title>
</head>
<body>
<div id="app">
  <h2>num1:{{num1}}</h2>
  <h2>num2:{{num2}}</h2>
  <hr>
  <cpn :number1="num1"
       :number2="num2"
       @number1change="num1Change"
       @number2change="num2Change"/>
</div>
<template id="cpn">
  <div>
    <h2>{{number1}}</h2>
    <input type="text" v-model="dnumber1">
    <h2>{{number2}}</h2>
    <input type="text" v-model="dnumber2">
  </div>
</template>

<script src="../js/vue.js"></script>
<script>

  const cpn = {
    template: '#cpn',
    props: {
      number1: Number,
      number2: Number
    },
    data() {
      return {
        dnumber1: this.number1,
        dnumber2: this.number2
      }
    },
    watch: {
      dnumber1(newValue) {
        this.dnumber2 = newValue * 100;
        this.$emit('number2change', this.dnumber2)
      },
      dnumber2(newValue) {
        this.dnumber1 = newValue / 100;
        this.$emit('number1change', this.dnumber1)
      }
    }
  }


  const app = new Vue({
    el: '#app',
    data: {
      num1: 1,
      num2: 2
    },
    components: {
      cpn: cpn
    },
    methods: {
      num1Change(value) {
        this.num1 = parseFloat(value);
      },
      num2Change(value) {
        this.num2 = parseFloat(value);
      }
    }
  });
</script>
</body>
</html>
